package com.practice.java8.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ForEachMain {

	public static void main(String[] args) {
		forEach();
		peek();
	}
	
	public static void forEach() {
		List<Person> roster = new ArrayList<>();
		// Java 8
		roster.stream()
			.filter(p -> p.getGender() == Person.Sex.MALE)
			.forEach(p -> System.out.println(p.getNumber()));
		// Pre-Java 8
		for (Person p : roster) {
			if (p.getGender() == Person.Sex.MALE) {
				System.out.println(p.getNumber());
			}
		}
	}
	
	public static void peek() {
		Stream.of("one", "two", "three", "four")
		 .filter(e -> e.length() > 3)
		 .peek(e -> System.out.println("Filtered value: " + e))
		 .map(String::toUpperCase)
		 .peek(e -> System.out.println("Mapped value: " + e))
		 .collect(Collectors.toList());
	}
	
	private static class Person {

		enum Sex {
			MALE, FEMALE
		}

		private Integer number;

		private Sex gender;

		public Person(Integer number, Sex gender) {
			this.number = number;
			this.gender = gender;
		}

		public Integer getNumber() {
			return number;
		}

		public Sex getGender() {
			return gender;
		}

	}
	
}
